/*
 * Copyright (c) 2009 Tim Mead
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 */

/**
 * Helper class for generating Dialogs.
 */
package com.misoneism.fire.util
{
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	
	import mx.containers.ControlBar;
	import mx.containers.HBox;
	import mx.containers.TitleWindow;
	import mx.controls.Button;
	import mx.controls.Image;
	import mx.core.Container;
	import mx.managers.PopUpManager;
	
	public class DialogUtil
	{
		// Dialog button defines
		/** An 'ok' button (validation). */
		public static const BUTTON_OK:uint = 0x01;
		/** A 'cancel' button (no validation). */
		public static const BUTTON_CANCEL:uint = 0x02;
		/** A 'close' button (no validation). */
		public static const BUTTON_CLOSE:uint = 0x04;
		
		// Dialog icon defines
		/** Infomation icon. */
		public static const ICON_INFO:uint = 0x0100;
		/** Question icon. */
		public static const ICON_QUESTION:uint = 0x0200;
		/** Warning icon. */
		public static const ICON_WARNING:uint = 0x0400;
		/** Error icon. */
		public static const ICON_ERROR:uint = 0x0800;
		
		// Dialog icons
		[Embed(source='../images/information-48.png')]
		private static const infoIcon:Class;
		[Embed(source='../images/question-48.png')]
		private static const questionIcon:Class;
		[Embed(source='../images/warning-48.png')]
		private static const warningIcon:Class;
		[Embed(source='../images/error-48.png')]
		private static const errorIcon:Class;
		
		/**
		 * Create and display a simple, self-contained dialog window.
		 */
		public static function showDialog(parent:DisplayObject, content:Container, title:String, modal:Boolean=false,
				dialogOptions:uint=BUTTON_OK, icon:Class=null, callback:Function=null):void
		{
			// Create a dialog to display to the user
			var dialog:TitleWindow = new TitleWindow();
			
			// Build a container to hold the dialog icon and the content
			var hbox:HBox = new HBox();
			var dialogIcon:Image = new Image();
			hbox.addChild(dialogIcon);
			dialog.addChild(hbox);
			
			// Setup the basic properties of the dialog
			dialog.title = title;
			hbox.addChild(content);
			if (icon != null)
				dialog.titleIcon = icon;
			if (callback != null)
				dialog.addEventListener(DialogEvent.DIALOG_CLOSED_EVENT, callback);
				
			// Create the button bar 
			var buttonBar:ControlBar = new ControlBar();
			buttonBar.setStyle("horizontalAlign", "right");
			dialog.addChild(buttonBar);
			
			// Check for accepting buttons
			var acceptButton:Button;
			if ((dialogOptions & BUTTON_OK) != 0)
			{
				acceptButton = new Button();
				acceptButton.label = "Ok";
			}
			
			// Check for cancelling buttons
			var cancelButton:Button;
			if ((dialogOptions & BUTTON_CANCEL) != 0)
			{
				cancelButton = new Button();
				cancelButton.label = "Cancel";
			}
			else if ((dialogOptions & BUTTON_CLOSE) != 0)
			{
				cancelButton = new Button();
				cancelButton.label = "Close";
			}
			
			// Setup the dialog icon
			if ((dialogOptions & ICON_INFO) != 0)
			{
				dialogIcon.source = infoIcon;
			}
			else if ((dialogOptions & ICON_QUESTION) != 0)
			{
				dialogIcon.source = questionIcon;
			}
			else if ((dialogOptions & ICON_WARNING) != 0)
			{
				dialogIcon.source = warningIcon;
			}
			else if ((dialogOptions & ICON_ERROR) != 0)
			{
				dialogIcon.source = errorIcon;
			}
			
			// Add buttons as necessary
			if (cancelButton != null)
			{
				cancelButton.addEventListener(MouseEvent.CLICK, function():void
				{
					PopUpManager.removePopUp(dialog);
					dialog.dispatchEvent(new DialogEvent(DialogEvent.DIALOG_CLOSED_EVENT, false, false,
						content, DialogUtil.BUTTON_CANCEL));
				});
				buttonBar.addChild(cancelButton);
			}
			
			if (acceptButton != null)
			{
				acceptButton.addEventListener(MouseEvent.CLICK, function():void
				{
					PopUpManager.removePopUp(dialog);
					dialog.dispatchEvent(new DialogEvent(DialogEvent.DIALOG_CLOSED_EVENT, false, false,
						content, DialogUtil.BUTTON_OK));
				});
				buttonBar.addChild(acceptButton);
			}
			
			// TODO: Invoke any validators found on the passed-in content
			
			// Show the dialog
			PopUpManager.addPopUp(dialog, parent, modal);
			PopUpManager.centerPopUp(dialog);
		}
	}
}